//index.js
//获取应用实例
import common from '../../utils/common.js'
const app = getApp()
const cateArr = [
  { "id": 7, "label": "轻奢" },
  { "id": 4, "label": "现代" },
  { "id": 2, "label": "中式" },
  { "id": 5, "label": "新中式" },
  { "id": 3, "label": "欧式" },
  { "id": 1, "label": "美式" },
  { "id": 6, "label": "北欧" },
]

Page({
  data: {
    cate: cateArr,
    openid: '44vpis+4qJhXwj3IXOgY',
    id: 7,
    styleName: '轻奢',
    imageArr: [],
    imgheights: [],
    isRuleTrue: false,
    imgwidth: 750,
    current: 0,
    first:0
  },
  onLoad:function(){
    //获取写入相册权限
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
             
            }
          })
        }
      }
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  sq:function(){
    var image = this.data.imageArr[this.data.current]
    var imageUrl = 'https://www.d-shang.com/' + image['source']
    common.sq(imageUrl)
  },
  imageLoad: function (e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    //把每一张图片的高度记录到数组里
    imgheights[e.target.dataset['index']] = imgheight;// 改了这里 赋值给当前 index
    this.setData({
      imgheights: imgheights,
    })
  },
  bindchange: function (e) {
    this.setData({
      current: e.detail.current
    })
  },
  onLoad: function () {
    this.getImageById()
  },
  handleClick: function (e) {
    var value = e.currentTarget.dataset.id
    this.setData({
      id: value.id,
      styleName: value.label,
      first:0
    })
    this.hideRule()
    this.getImageById()
  },
  getImageById: function () {
    var that = this
    var url = 'https://www.d-shang.com/index.php?fangan/getdesginbystyle/?openid=' +
      this.data.openid +
      '&p=' +
     1 +
      '&style=' +
      this.data.id;
    wx.request({
      url: url,// 仅为示例，并非真实的接口地址
      success(res) {
        var d = res.data.data
        console.log(d)
        that.setData({
          imageArr: d.data
        })
      }
    })
  },
  filter: function () {
    this.setData({
      isRuleTrue: true
    })
  },
  hideRule: function () {
    this.setData({
      isRuleTrue: false
    })
  },
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '顶上整装定制',
      path: 'pages/walls/walls',
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }

  }
})